package models.games;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

import core.User;

public class MemoriaOrdinata extends Game {
	public final int POINTS_PER_ROUND = 100;
	public int HOW_MANY_NUMBERS_TO_SORT = 4;

	private int[] sortedNumbers; // numeri generati ad ogni round numeri
									// inseriti dall'utente
	private int sortedNumbersIndex;
	private int[] shuffledNumbers;
	// private int numbersSize; // TODO nei round successivi si potrebbe
	// aumentare
	// // la quantità di numeri e quindi mantenere il campo size
	private Random random;
	private int round;

	private Scanner sc = new Scanner(System.in);

	public MemoriaOrdinata(User user) {
		super(user);
		sortedNumbers = new int[HOW_MANY_NUMBERS_TO_SORT];
		shuffledNumbers = new int[HOW_MANY_NUMBERS_TO_SORT];
		round = 1;
		random = new Random();
		generateNumbers();
	}

	public void startGame() {
		System.out.println("Round: " + round);
		while (round < 6) {

			System.out.println("Numeri: " + Arrays.toString(shuffledNumbers));
			try {
				if (elaborateAnswer(sc.nextInt())) {
					System.out.println("Risposta corretta");
				} else {
					System.out.println("Risposta errata");
				}
			} catch (Exception e) {
				System.out.println("Errore, scrivi il numero");
			}
		}
		System.out.println("FINE MEMORIA ORDINATA");
		System.out.println("PUNTEGGIO FINALE = " + score);

	}

	private void generateNumbers() {
		System.out.println("generati numeri");
		int actualNumber = 0;
		for (int i = 0; i < sortedNumbers.length; i++) {
			sortedNumbers[i] = actualNumber + 1 + random.nextInt(3);
			actualNumber = sortedNumbers[i];
		}
		sortedNumbersIndex = 0;
		shuffle();
	}

	public boolean elaborateAnswer(int typedNumber) {
		if (typedNumber == sortedNumbers[sortedNumbersIndex]) { // risposta
																// esatta
			if (sortedNumbersIndex == sortedNumbers.length - 1) { // round
																	// completato
																	// correttamente
				score = score + 250 + 250 * level;
				round++;
				System.out.println("Round : " + round);
				refresh(true);
				generateNumbers();
			} else {
				sortedNumbersIndex++;
			}
			return true;
		} else {
			round++;
			refresh(false);
			generateNumbers();
			return false;
		}
	}

	private void shuffle() { // genera shuffledNumbers a partire da
								// sortedNumbers
		for (int i = 0; i < shuffledNumbers.length; i++) {
			shuffledNumbers[i] = sortedNumbers[i];
		}
		for (int i = 0; i < shuffledNumbers.length - 1; i++) {
			int randomIndex = i + 1
					+ random.nextInt(shuffledNumbers.length - i - 1);
			int tmp = shuffledNumbers[i];
			shuffledNumbers[i] = shuffledNumbers[randomIndex];
			shuffledNumbers[randomIndex] = tmp;
		}
	}

	private void refresh(boolean win) {
		if (win)
			HOW_MANY_NUMBERS_TO_SORT++;
		else if (HOW_MANY_NUMBERS_TO_SORT > 4) {
			HOW_MANY_NUMBERS_TO_SORT--;
		}
		sortedNumbers = new int[HOW_MANY_NUMBERS_TO_SORT];
		shuffledNumbers = new int[HOW_MANY_NUMBERS_TO_SORT];
	}

	public static void main(String[] args) {
		MemoriaOrdinata m = new MemoriaOrdinata(null);
		m.startGame();

	}

	public int[] getShuffledNumbers() {
		return shuffledNumbers;
	}

	public int getNumbersToSort() {
		return HOW_MANY_NUMBERS_TO_SORT;
	}

	@Override
	public int getGameKey() {
		return 9;
	}

	public String toString() {
		return "Ciao da MemoriaOrdinataModel \n shuffledNumbers = "
				+ Arrays.toString(shuffledNumbers);
	}
}
